package com.ukao.cashregister.ui.splash;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.ukao.cashregister.R;
import com.ukao.cashregister.base.BaseFragment;
import com.ukao.cashregister.bean.SearchHistorysBean;
import com.ukao.cashregister.bean.StringBean;
import com.ukao.cashregister.db.SearchHistorysDao;
import com.ukao.cashregister.retrofit.ApiCallback;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.L;
import com.ukao.cashregister.utils.T;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


public class SerialumberFragment extends BaseFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.back_btn)
    TextView backBtn;
    @BindView(R.id.splash_title)
    TextView splashTitle;
    @BindView(R.id.serial_num)
    EditText serialNum;
    @BindView(R.id.actCode)
    EditText actCode;
    Unbinder unbinder;
    @BindView(R.id.sure_btn)
    TextView sureBtn;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private SearchHistorysDao dao;
    private ArrayList<SearchHistorysBean> historywordsList;
    private String serialumber;


    public static SerialumberFragment newInstance(String param1, String param2) {
        SerialumberFragment fragment = new SerialumberFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_serialumber, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }


    @Override
    protected void initView() {
        dao= new SearchHistorysDao(_mActivity);
        historywordsList= dao.findAll();
        serialumber="";
        for (SearchHistorysBean searchHistorysBean : historywordsList) {
             serialumber=serialumber+searchHistorysBean.historyword;
        }
        L.i("serialumber="+serialumber);

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({ R.id.sure_btn,R.id.back_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case   R.id.back_btn:
                    finish();
                break;

            case R.id.sure_btn:
                String   strSerialNum=getText(serialNum);
                String   stractCode=getText(actCode);
                if (CheckUtils.isEmpty(strSerialNum)){

                    T.show("请输入系列号");
                    return;
                }else if (CheckUtils.isEmpty(stractCode)){
                    T.show("请输入验证码");
                    return;
                }


                setSerialNum(strSerialNum,stractCode);
                break;
        }
    }

    /**
     *获取用户信息
     */
    public void setSerialNum (final  String  strSerialNum,final String stractCode   ) {
        showProgressDialog();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("serialNum",strSerialNum);
        mHashMap.put("actCode",stractCode);
        L.i("mHashMap="+mHashMap.toString());
        addSubscription(apiStores().serialNum(mHashMap),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {

                        if (model.getHttpCode()==200){


                            dao.deleteAll();
                            addSeranum(strSerialNum, stractCode);


                        }


                        T.show(model.getMsg());
                    }



                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                    }

                });
    }

    private void addSeranum(String strSerialNum, String stractCode) {
        JSONObject mJSONObject=new JSONObject();
        try {
            mJSONObject.put("serialNum",strSerialNum);
            mJSONObject.put("actCode",stractCode);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        dao.addOrUpdate(mJSONObject.toString());
        start(ClauseFragment.newInstance("", ""));
    }
}
